//
//  P1088dfs.cpp
//  Algorithm
//
//  Created by 刘锦程 on 2023/9/10.
//

#include <stdio.h>
#include<iostream>
using namespace std;
int N,M,finger[10003];
int book[10003],output[10003];
int flag=0;
void dfs(int s){
    if(s==N){
        flag++;
        if(flag==M+1){
            for(int z=0;z<N;z++){
                printf("%d ",finger[z]);
            }
            printf("\n");
            exit(0);
        }
        return;
    }
    for(int i=1;i<=N;i++){
        if(flag){
            if(!book[i]){
                book[i]=1;
                finger[s]=i;
                dfs(s+1);
                book[i]=0;
                finger[s]=0;
                
            }
        }else{
            i=finger[s];
                book[i]=1;
                dfs(s+1);
                book[i]=0;
                
            }
        }
    }



int P1088dfsans(){
    cin>>N>>M;
    for(int i=0;i<N;i++){
        cin>>finger[i];
    }
    dfs(0);
    
    return 0;
}
